//
//  GymInfoNavViewController.m
//  YouthOlympic2014
//
//  Created by xiangpaopao on 14-6-30.
//  Copyright (c) 2014年 mitian. All rights reserved.
//

#import "GymInfoNavViewController.h"
#import "UIView+WhenTappedBlocks.h"
#import "GymAnnotation.h"
#import "GymAnnotationView.h"
#import "GymMatchCell.h"
#import "PlaceService.h"
#import "GameService.h"
#import "MBProgressHUD.h"

@interface GymInfoNavViewController ()
{
    MKCoordinateRegion newRegion;
}

@property(nonatomic, strong)MBProgressHUD *HUD;

@property (nonatomic, strong) NSMutableArray* annotationArray;
@property (nonatomic, strong) NSArray*/*SubPlace*/ locationArray;
@property (nonatomic, strong) NSArray* matchArray;

@end

@implementation GymInfoNavViewController
@synthesize locationArray,matchArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setTitle:@"场馆信息"];
    
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    self.HUD.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:self.HUD];
    
    [self initAnnotation];
    
    [self setupActivityView];
}


- (void)initAnnotation{
    self.annotationArray = [NSMutableArray arrayWithCapacity:10];

    [self.HUD show:YES];
    [[[PlaceService alloc] init] getAllSubPlaces:@"" completion:^(id result, NSError *error) {
        
        [self.HUD hide:YES];
        
        if (nil == error) {
            self.locationArray = result;
            
            for (int i = 0; i<[self.locationArray count]; i++) {
                SubPlace* subplace = self.locationArray[i];
                NSString *lId = subplace.id;
                NSString *name =  subplace.name;
                //NSString *address = [[dataArray objectAtIndex:i] valueForKey:@"address"];
                double lat = [subplace.latitude doubleValue];
                double lon =  [subplace.longtitude doubleValue];
                GymAnnotation* annotation = [[GymAnnotation alloc] init];
                [annotation setTitle:name];
                [annotation setLId:lId];
                [annotation setCoordinate:CLLocationCoordinate2DMake(lat , lon)];
                annotation.icon = [UIImage imageNamed:subplace.picTag];
                annotation.placeId = subplace.picTag;
                annotation.subplaceId = subplace.id;
                [self.annotationArray addObject:annotation];
                [self.mapView addAnnotations:self.annotationArray];
            }
            
            NSLog(@"result is: %@",[AppUtils replaceUnicodeDic:result]);
        }else{
            //TODO
        }
        
        
    }];

    [self.mapView addAnnotations:self.annotationArray];
    [self gotoLocation];
}

- (void)gotoLocation
{
    newRegion.center.latitude = 32.062064;
    newRegion.center.longitude = 118.797027;
    newRegion.span.latitudeDelta = 0.28;
    newRegion.span.longitudeDelta = 0.28;
    [self.mapView setRegion:newRegion animated:YES];
}

#pragma mark - MapView Delegate
- (MKAnnotationView *)mapView:(MKMapView *)theMapView
            viewForAnnotation:(id <MKAnnotation>)annotation{
    // if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    static NSString* annotationIdentifier = @"annotationIdentifier";
    GymAnnotationView* pinView = (GymAnnotationView *)
    [_mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
    if (!pinView)
    {
        GymAnnotationView* customPinView = [[GymAnnotationView alloc]
                                               initWithAnnotation:annotation
                                               reuseIdentifier:annotationIdentifier];
        [customPinView initAnnotationView];
        customPinView.canShowCallout = NO;
        
        return customPinView;
    }else {
        pinView.annotation = annotation;
    }
    return pinView;
}

#pragma mark - ActivityView
-(void)setupActivityView
{
    self.infoView.frame = CGRectMake(0, ScreenHeight, self.infoView.frame.size.width, self.infoView.frame.size.height);
    self.shadeView.hidden = YES;
    [self.shadeView whenTapped:^{
        [self hideActivityView];
        if([[self.mapView selectedAnnotations] count] > 0)
        {
            [self.mapView deselectAnnotation:[[self.mapView selectedAnnotations] objectAtIndex:0] animated:NO];
        }
    }];
}
-(void)showActivityView{
    self.mapView.userInteractionEnabled=NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.shadeView.hidden = NO;
        self.infoView.frame = CGRectMake(0, ScreenHeight-200 -64, self.infoView.frame.size.width, self.infoView.frame.size.height);
        
        self.shadeView.alpha = 0;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            self.shadeView.alpha = 0.2;
        }];
        [self.infoListView reloadData];
    }];
}

-(void)hideActivityView{
    [UIView animateWithDuration:0.3 animations:^{

        self.shadeView.alpha = 0.2;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            
            self.shadeView.alpha = 0.0;
            self.shadeView.hidden = YES;
            self.infoView.frame = CGRectMake(0, ScreenHeight, self.infoView.frame.size.width, self.infoView.frame.size.height);
            self.matchArray = nil;
            [self.infoListView reloadData];
        }];
    }];
    
    self.mapView.userInteractionEnabled=YES;
}


-(IBAction)zoomLevelUp:(id)sender
{
    if(newRegion.span.latitudeDelta >0.005)
    {
        newRegion.span.latitudeDelta *= 0.5;
        newRegion.span.longitudeDelta *= 0.5;
        [self.mapView setRegion:newRegion animated:YES];
    }
}
-(IBAction)zoomLevelDown:(id)sender
{
    if(newRegion.span.latitudeDelta <0.6)
    {
        newRegion.span.latitudeDelta /= 0.5;
        newRegion.span.longitudeDelta /= 0.5;
        [self.mapView setRegion:newRegion animated:YES];
    }
}


#pragma mark MKMapViewDeletate

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    GymAnnotation* annotation = (GymAnnotation*)view.annotation;
    self.infoTitle.text = annotation.title;
    self.infoSubTitle.text = @"";
    self.infoLabel.text = @"今日场馆内的比赛:";
    [self.infoImgView setImage:annotation.icon];
    //
    [[[GameService alloc] init] getGameEventsByPlaceId:annotation.placeId
                                         andSubPlaceId:annotation.subplaceId
                                               andDate:[AppUtils getCurYMDDateStrInYogRange]
                                            completion:^(id result, NSError* error){
                                                self.matchArray = result;
                                                [self.infoListView reloadData];
    }];
    //
    [self showActivityView];
};


- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    
}


#pragma mark - TableView delegate & datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    return self.matchArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    GymMatchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"gymMatchCell" forIndexPath:indexPath];

    GameEvent* gameEvent = self.matchArray[indexPath.row];
    [cell.titleLabel setText: gameEvent.title];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}




@end
